home *** CD-ROM | disk | FTP | other *** search
- Path: coranto.ucs.mun.ca!usenet
- From: saustin@terra.nlnet.nf.ca (Steve Austin)
- Newsgroups: comp.lang.c++
- Subject: Re: References & Pointers in Borland C++
- Date: Wed, 13 Mar 1996 02:04:15 GMT
- Organization: Kickham Productions
- Message-ID: <4i5aem$9e7@coranto.ucs.mun.ca>
- References: <4i26j5$roe@dfw-ixnews3.ix.netcom.com>
- NNTP-Posting-Host: n104h130.nlnet.nf.ca
- X-Newsreader: Forte Agent .99b.112
-
- bob_m@ix.netcom.com(Bob M ) wrote:
-
- >I am trying to understand the concept of "References".
-
- Me too.
-
- >1.
- >The Programmer's manuals for Borland Turbo C++ version 1.01 (1990)
- >and Borland C++ version 4.02 describe in nearly identical text, that
- >it is acceptable to initialize a Reference with a specific number:
- >int& ir = 6;
- >Both my 3.1 and 4.02 compilers reject this with an error: "Reference
- >initialized with 'int' needs lvalue of type 'int'".
- >Which is right, the compiler or the book?
-
- I tried this with BCC 4.02 and just got the warning:
- "Temporary used to initialize "ir" in function..."
-
- This just means that the reference "ir" is bound to an anonymous
- temporary, which is not otherwise used. I couldn't reproduce your
- error message.
-
- >2.
- >Using the Inspector window in the Debugger, I could see that a
- >Reference appears identical to a pointer, except that the compiler
- >insists that you initialize it to some selected variable.
- >It looks as though one should be able to assign a "filled in"
- >pointer to a reference, as follows:
- >int num, nother;
- >int &ref1 = num, &ref2 = nother;
- >int *ptr1;
- >
- >ptr1 = &ref1; // sets the pointer to the same address as "num".
- >&ref2 = ptr1; // Change &ref2 to point at "num".
- >
- >The first assignment is apparently legal, but the second depends
- >on the version of the compiler. 3.1 accepts it, while 4.01 says:
- >"Lvalue required in function main".
- >Should this be legal?
-
- As far as I can see, the second assignment tries to set the address of
- the object bound to ref2 (i.e. in this case, the address of num) to a
- pointer to an integer, which is a meaningless operation since &ref2 is
- not a modifiable lvalue and therefore can't appear on the left side of
- an expression.
-
- A reference has to be initialized, and then is bound to the same
- object for the duration of it's scope. You can't bind "ref2" to "num",
- after you've initialized it to "nother".
-
- I think the confusion partly arises from the use of "&" to mean two
- different but related things, just like with "*"; and from the habit
- of writing "int &" instead of "int&", which is clearer.
-
- For example "int *px" at least has the merit that "*px" by itself is
- an int ( this is the justification given by K&R for writing it this
- way), but given "int &ir = i", by itself "&ir" is *not* an int.
-
- Better I think to avoid this stylistic quirk and write "int* px"
- meaning "px" is a pointer to an integer, and "int& ir = i", meaning
- "ir" is a reference to an integer.
-
- Hope this helps
-
- Steve
-
-
-